class QueueElement {
  constructor(element, priority) {
    this.element = element;
    this.priority = priority;
  }
}
export default class PriorityQueue {
  constructor() {
    this.arr = [];
  }
  enqueue(element, priority) {
    let queueElement = new QueueElement(element, priority);
    if (this.arr.length === 0) {
      this.arr.push(queueElement);
    } else {
      let isAdded = false;
      for (let i = 0; i < this.arr.length; i++) {
        if (queueElement.priority < this.arr[i].priority) {
          this.arr.splice(i, 0, queueElement);
          isAdded = true;
          break;
        }
      }
      if (!isAdded) {
        this.arr.push(queueElement);
      }
    }
  }
  // 出栈
  dequeue() {
    return this.arr.shift();
  }
  // 返回队列第一个元素
  front() {
    return this.arr[0];
  }
  isEmpty() {
    return this.arr.length === 0;
  }
  size() {
    return this.arr.length;
  }
  toString() {
    var str = "";
    for (var i = 0; i < this.arr.length; i++) {
      str += this.arr[i].priority + "-" + this.arr[i].element + " ";
    }
    return str;
  }
}
